<?xml version="1.0"?>
<!--
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
-->
<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/2005/07/scxml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
  xmlns:uml="http://www.eclipse.org/uml2/3.0.0/UML" xmlns:scxml="http://www.w3.org/2005/07/scxml">

  <xsl:output method="xml" indent="yes" encoding="UTF-8" />

  <xsl:template match="uml:Package">
<xsl:comment>
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements. See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
</xsl:comment><xsl:text>&#xA;</xsl:text>
<xsl:comment>
  This file was generated by a stylesheet from the Apache Commons SCXML
  project: https://commons.apache.org/scxml
</xsl:comment><xsl:text>&#xA;</xsl:text>
    <scxmlfiles xmlns="">
      <xsl:apply-templates select="packagedElement" />
    </scxmlfiles><xsl:text>&#xA;</xsl:text>
  </xsl:template>

  <xsl:template match="packagedElement">
    <xsl:variable name="type" select="@xmi:type" />
    <xsl:variable name="name" select="@name" />
    <xsl:variable name="filename" select="concat($name,'.xml')" />
    <file xmlns="" uml2name="{$name}" scxmlfile="{$filename}" />
    <xsl:result-document href="{$filename}" method="xml">
<xsl:comment>
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements. See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
</xsl:comment><xsl:text>&#xA;</xsl:text>
<xsl:comment>
  This file was generated by a stylesheet from the Apache Commons SCXML
  project: https://commons.apache.org/scxml
</xsl:comment><xsl:text>&#xA;</xsl:text>
      <xsl:choose>
        <xsl:when test="count(child::region) &gt; 1">
          <scxml version="1.0"
            initial="__root">
            <parallel id="__root">
              <xsl:apply-templates select="region">
                <xsl:with-param name="orth" select="true()"/>
              </xsl:apply-templates>
            </parallel>
          </scxml><xsl:text>&#xA;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <scxml version="1.0"
            initial="{region/subvertex[@xmi:type='uml:Pseudostate'][empty(@kind)]/@xmi:id}">
            <xsl:apply-templates select="region">
              <xsl:with-param name="orth" select="false()"/>
            </xsl:apply-templates>
          </scxml><xsl:text>&#xA;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:result-document>
  </xsl:template>

  <xsl:template match="region">
    <xsl:param name="orth" />
    <xsl:choose>
      <xsl:when test="$orth">
        <state id="{concat('__region',position())}" initial="{subvertex[@xmi:type='uml:Pseudostate'][empty(@kind)]/@xmi:id}">
          <xsl:apply-templates select="subvertex" />
        </state>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="subvertex" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="subvertex">
    <xsl:variable name="type" select="@xmi:type" />
    <xsl:variable name="xmiid" select="@xmi:id" />
    <xsl:variable name="name" select="@name" />
    <xsl:variable name="id" select="if (empty($name)) then $xmiid else $name" />
    <xsl:choose>
      <xsl:when test="$type='uml:Pseudostate'">
        <xsl:choose>
          <xsl:when test="@kind='deepHistory'">
            <history type="deep" id="{$id}">
              <xsl:apply-templates select="../transition[@source=$xmiid]" />
            </history>
          </xsl:when>
          <xsl:when test="@kind='shallowHistory'">
            <history type="shallow" id="{$id}">
              <xsl:apply-templates select="../transition[@source=$xmiid]" />
            </history>
          </xsl:when>
          <xsl:otherwise>
            <state id="{$id}">
              <xsl:apply-templates select="../transition[@source=$xmiid]" />
            </state>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$type='uml:State'">
        <state id="{$id}">
          <xsl:apply-templates select="../transition[@source=$xmiid]" />
        </state>
      </xsl:when>
      <xsl:when test="$type='uml:FinalState'">
        <final id="{$id}" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="transition">
    <xsl:variable name="target" select="@target" />
    <xsl:variable name="tname" select="if (empty(../subvertex[@xmi:id=$target]/@name)) then ../subvertex[@xmi:id=$target]/@xmi:id else ../subvertex[@xmi:id=$target]/@name" />
    <xsl:choose>
      <xsl:when test="empty(@name)">
        <transition target="{$tname}" />
      </xsl:when>
      <xsl:otherwise>
        <transition event="{@name}" target="{$tname}" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
